package yunwo.cn.yzstation.model;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Request;
import yunwo.cn.yzstation.api.HttpManager;
import yunwo.cn.yzstation.api.IshoudongModel;
import yunwo.cn.yzstation.api.okapi.GLCallBack;
import yunwo.cn.yzstation.base.Yzapp;
import yunwo.cn.yzstation.bean.AssigmnetBean;
import yunwo.cn.yzstation.bean.ContinueBean;
import yunwo.cn.yzstation.bean.EndOrganbean;
import yunwo.cn.yzstation.bean.ItemlistBean;
import yunwo.cn.yzstation.bean.StringBean;
import yunwo.cn.yzstation.config.MyConstants;
import yunwo.cn.yzstation.presenter.ShoudongPersenter;
import yunwo.cn.yzstation.utils.StringUtil;
import yunwo.cn.yzstation.view.activity.LoginActivity;
import yunwo.cn.yzstation.view.activity.ShoudongActivity;

/**
 * Created by lao on 2017/10/11.
 */
public class ShoudongMdoel {
    ShoudongActivity shoudongActivity;
    IshoudongModel ishoudongModel;
    private Dialog mMyDialog;

    public ShoudongMdoel(ShoudongActivity shoudongActivity, IshoudongModel ishoudongModel) {
        this.shoudongActivity = shoudongActivity;
        this.ishoudongModel = ishoudongModel;

    }

    public void getoBtainTrack(AssigmnetBean assigmnetBean,ContinueBean.DataBean dataBean) {
        Map<String, Object> map = new HashMap<>();
        String carobjId = "";
        if (assigmnetBean != null) {
            carobjId   = assigmnetBean.getCarobjId();
        }else {
            carobjId =  dataBean.getTruckTargetId();
        }
        Log.d("sadsad", "dddddddd"+carobjId);
        map.put("beginOrganId",carobjId );
        map.put("status", 4);
        map.put("isShort", "1");

        HttpManager.hpptPostString3(MyConstants.obtainTrack, map).execute(new GLCallBack() {

            private List<ItemlistBean.DataBean> mDataBeanList;

            @Override
            public void onResponseT(String result, int code, String msg, int id) {
                if (code == 200) {
                    if (!StringUtil.isEmpty(result)) {
                        mDataBeanList = new Gson().fromJson(result, new TypeToken<List<ItemlistBean.DataBean>>() {
                        }.getType());
                        ishoudongModel.getoBtainTrackSueecss(mDataBeanList);
                    }
                } else {
                    Toast.makeText(shoudongActivity, msg, Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(Call call, Exception e, int id) {
                Toast.makeText(shoudongActivity, "出错了", Toast.LENGTH_SHORT).show();
                ishoudongModel.getoBtainTrackfailure();
            }
        });
    }

    public void complete(AssigmnetBean assigmentBean,
                         List<ItemlistBean.DataBean> selectedCollectedCars
            , ContinueBean.DataBean mMContinueBean) {

        Map<String, Object> map = new HashMap<>();


        if (mMContinueBean != null) {
            map.put("batchNumber", mMContinueBean.getBatchNumber());
            map.put("departDetailList", selectedCollectedCars);
            map.put("id", mMContinueBean.getDepartId());
            map.put("driverName", mMContinueBean.getDriverName());
            map.put("driverPhone", mMContinueBean.getDriverPhone());
            map.put("isShort", 1);
            map.put("plateNumber", mMContinueBean.getPlateNumber());
            map.put("vehicleType", mMContinueBean.getVehicleType());
            map.put("truckTarget", mMContinueBean.getTruckTarget());
            map.put("truckTargetId", mMContinueBean.getTruckTargetId());
        }else {
            map.put("batchNumber", assigmentBean.getBatch());
            map.put("departDetailList", selectedCollectedCars);
//        map.put("id", mMContinueBean.getDepartId());
            map.put("driverId", assigmentBean.getDriverId());
            map.put("driverName", assigmentBean.getDriver());
            map.put("driverPhone", assigmentBean.getDriverphone());
            map.put("isShort", 1);
            map.put("plateNumber", assigmentBean.getCarnumber());
            map.put("vehicleId", assigmentBean.getCarId());
            map.put("vehicleType", assigmentBean.getCartypeId());
            map.put("truckTarget", assigmentBean.getCarobj());
            map.put("truckTargetId", assigmentBean.getCarobjId());
        }



        HttpManager.hpptPostString3(MyConstants.confirmPz, map).execute(new GLCallBack() {
            @Override
            public void onResponseT(String result, int code, String msg, int id) {
                if (code == 200) {
                    Toast.makeText(shoudongActivity, "配载成功", Toast.LENGTH_SHORT).show();
                    ishoudongModel.completeSueescc();
                } else {
                    Toast.makeText(shoudongActivity, msg, Toast.LENGTH_SHORT).show();
                }
            }
            @Override
            public void onError(Call call, Exception e, int id) {
                Toast.makeText(shoudongActivity, "网络出错了", Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void Addarea(AssigmnetBean assigmentBean, ContinueBean.DataBean mContinueBean, List<ItemlistBean.DataBean> selectedCollectedCars) {
        Map<String, Object> map = new HashMap<>();
        if (assigmentBean != null) {
            map.put("batchNumber", assigmentBean.getBatch());
            map.put("departDetailList", selectedCollectedCars);
            map.put("driverId", assigmentBean.getDriverId());
            map.put("driverName", assigmentBean.getDriver());
            map.put("driverPhone", assigmentBean.getDriverphone());

            map.put("isShort", 1);
            map.put("loadingCharges", "");
            map.put("plateNumber", assigmentBean.getCarnumber());
            map.put("truckTarget", assigmentBean.getCarobj());
            map.put("truckTargetId", assigmentBean.getCarobjId());
            map.put("vehicleId", assigmentBean.getCarId());
            map.put("vehicleType", assigmentBean.getCartypeId());




            HttpManager.hpptPostString3(MyConstants.stowageconfirm,map).execute(new GLCallBack() {
                @Override
                public void onResponseT(String result, int code, String msg, int id) {
                    if (code == 200) {
                        if (!StringUtil.isEmpty(result)) {
                            String departId = result.replace("\"", "");
                            ishoudongModel.AddareaSuccess(departId);
                        }
                    }else {
                        ishoudongModel.Addarefailure();
                        Toast.makeText(shoudongActivity, msg, Toast.LENGTH_SHORT).show();
                    }



                }

                @Override
                public void onError(Call call, Exception e, int id) {
                    ishoudongModel.Addarefailure();
                    Toast.makeText(shoudongActivity, "服务器异常，加入选定区失败", Toast.LENGTH_SHORT).show();

                }



                @Override
                public void onAfter(int id) {
                    if (mMyDialog != null && mMyDialog.isShowing()) {
                        mMyDialog.dismiss();
                    }
                }

                @Override
                public void onBefore(Request request, int id) {
                    if (mMyDialog == null) {
                        mMyDialog = ProgressDialog.show(shoudongActivity, null, "请稍等...");
                        mMyDialog.setCancelable(false);
                        mMyDialog.show();
                    }
                }

            });


/*new GLCallBack() {
                @Override
                public void onResponseT(String result, int code, String msg, int id) {

                    if (code == 200) {
                        ishoudongModel.AddareaSuccess();
                    }else {
                        ishoudongModel.Addarefailure();
                        Toast.makeText(shoudongActivity, msg, Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void onError(Call call, Exception e, int id) {
                    ishoudongModel.Addarefailure();
                    Toast.makeText(shoudongActivity, "服务器异常，加入选定区失败", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onAfter(int id) {
                    if (mMyDialog != null && mMyDialog.isShowing()) {
                        mMyDialog.dismiss();
                    }
                }

                @Override
                public void onBefore(Request request, int id) {
                    if (mMyDialog == null) {
                        mMyDialog = ProgressDialog.show(shoudongActivity, null, "请稍等...");
                        mMyDialog.setCancelable(false);
                        mMyDialog.show();
                    }
                }


            }*/





        }

    }
}
